
/*
 * 
 * This class describes the structure of a workflow that can be transformed into a BPEL
 * 
 */

package it.uniroma2.structure;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;


public abstract class Node implements BPEL_Element, Serializable{
	List<Node> subNodes;
	Node father;
	String name;
	private String defaultName = "name";
	private String orderId;
	static int indexName = 1;
	

	public Node(){
		subNodes = new ArrayList<Node>();
		this.name = this.defaultName + Node.indexName;
		Node.indexName++;
	}
	
	
	public Node(String name){
		this.name = name;
		subNodes = new ArrayList<Node>();
	}
	
	
	public String getName(){
		return this.name;
	}
	
	
	private boolean isContain(Vector<Service> services, Service s){
		for(Service temp : services){
			if(temp.equals(s)){
				return true;
			}
		}
		return false;
	}
	
	
	public void getServices(Vector<Service> services){
		for(Node tempNode : this.subNodes){
			if(tempNode instanceof Service){
				
				if(!isContain(services, (Service)tempNode)){
					services.addElement((Service)tempNode);
				}
			}else{
				tempNode.getServices(services);
			}
		}
			
	}
	
	
	public void getAllNodes(Vector<Node> nodes){
		if(!nodes.contains(this)){
			nodes.addElement(this);
		}
		for(Node tempNode : this.subNodes){
			if(tempNode instanceof Service){
				if(!nodes.contains(tempNode)){
					nodes.addElement((Service)tempNode);
				}
			}else{
				if(!nodes.contains(tempNode)){
					nodes.addElement(tempNode);
				}
				tempNode.getAllNodes(nodes);
			}
		}
			
	}
	
	
	public abstract String toBPEL();
	
	
	@Override
	public String toString() {
		return "name=\"" + this.name + "\"";
	}
	
	
	public String getOrderId() {
		return this.orderId;
	}

	
	public void setOrderId(String newOrder) {
		this.orderId = newOrder;
		
	}
	
	
	public void incrementOrderId() {
		int myid = Integer.parseInt(this.getOrderId().substring(this.getOrderId().lastIndexOf("-") + 1)) + 1;
		String newOrderId = this.getOrderId().substring(0,this.getOrderId().lastIndexOf("-") + 1) + myid;
		setOrderId(newOrderId);
		for(Node temp : this.subNodes){
			temp.updateOrderId(newOrderId);
		}
	}
	
	
	public void decrementOrderId() {
		int myid = Integer.parseInt(this.getOrderId().substring(this.getOrderId().lastIndexOf("-") + 1)) - 1;
		String newOrderId = this.getOrderId().substring(0,this.getOrderId().lastIndexOf("-") + 1) + myid;
		setOrderId(newOrderId);
		for(Node temp : this.subNodes){
			temp.updateOrderId(newOrderId);
		}
	}

	
	public void updateOrderId(String fatherId) {
		int myid = Integer.parseInt(this.getOrderId().substring(this.getOrderId().lastIndexOf("-")));
		String newOrderId = fatherId + "-" + myid;
		this.setOrderId(newOrderId);
		for(Node temp : this.subNodes){
			temp.updateOrderId(newOrderId);
		}
	}


	public void setFather(Node father) {
		this.father = father;
	}
	
	
	public Node getFatherNode() {
		return this.father;
	}


//	public abstract boolean validate();


}
